package com.hc.service;

import com.hc.bean.SysMenuBean;
import com.hc.mapper.SysMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenuSerivceImpl implements  MenuSerivce{




    @Autowired
    SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenuBean> queryDataByLoginUserId(Integer user_id) {
        List<SysMenuBean> sysMenuBeanList =  sysMenuMapper.queryDataByLoginUserId(user_id);
        List<SysMenuBean> returnMenuList = this.generatorTreeData(sysMenuBeanList);
        return returnMenuList;
    }

    @Override
    public int deleteMenuById(Integer id) {
        return 0;
    }


    private  List<SysMenuBean> generatorTreeData(List<SysMenuBean> sysMenuBeanList){
        List<SysMenuBean> returnMenuList = new ArrayList<>();
        //把服务端返回的查询结果中的一级菜单独取出，放一个集合中
        for(SysMenuBean levelOne:sysMenuBeanList){
            if (levelOne.getParent_id().equals(0)){
                returnMenuList.add(levelOne);
                for(SysMenuBean levelTwo:sysMenuBeanList){//找目录(一级菜单)下的子菜单(二级菜单)
                    if (levelTwo.getParent_id().equals(levelOne.getMenu_id())){
                        levelOne.getChildren().add(levelTwo);
                        for(SysMenuBean levelThree:sysMenuBeanList){//找菜单(二级菜单)下的子菜单(三级菜单)
                            if (levelThree.getParent_id().equals(levelTwo.getMenu_id())){
                                levelTwo.getChildren().add(levelThree);
                            }
                        }
                    }


                }

            }
        }
        return  returnMenuList;
    }
    @Override
    public List<SysMenuBean> queryAllDataByTree() {
        List<SysMenuBean> sysMenuBeanList = sysMenuMapper.queryData(null);


        List<SysMenuBean> returnMenuList = this.generatorTreeData(sysMenuBeanList);

        return returnMenuList;
    }
}
